<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2021/7/1
  Time: 9:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>$</title>
    <link rel="stylesheet" href="css/css.css">
    <link rel="stylesheet" href="css/img.css">
    <script src="js/jquery-1.8.2.min.js"></script>
    <script src="js/fileupload.js"></script>
    <script>
        $(function () {
            $.ajax({
                url:"brand.do",
                type:"post",
                dataType:"json",
                success:function (obj) {
                    for (let x in obj) {
                        $("[name='bid']").append("<option value="+obj[x].bid+">"+obj[x].bname+"</option>");
                    }
                }
            })
            $.ajax({
                url:"getById.do",
                data:{"sid":${param.sid}},
                type:"post",
                dataType:"json",
                success:function (obj) {
                    $("[name='sname']").val(obj.sname);
                    $("[name='title']").val(obj.title);
                    $("#atype").val(obj.atype);
                    $("#btype").val(obj.btype);
                    $("#ctype").val(obj.ctype);
                    $("[value='"+obj.status+"']").prop("checked",true);
                    $("[value='"+obj.bid+"']").prop("selected",true);
                    $("img").prop("src",obj.pic);
                }
            })
            $.ajax({
                url:"typess.do",
                data:{"pid":0},
                type:"post",
                dataType:"json",
                success:function (obj) {
                    for (let x in obj) {
                        if (obj[x].id==$("#atype").val()){
                            $("[name='atype']").append("<option value="+obj[x].id+" selected>"+obj[x].name+"</option>");
                            $("[name='atype']").trigger("change");
                        }else {
                            $("[name='atype']").append("<option value="+obj[x].id+">"+obj[x].name+"</option>");
                        }
                    }
                }
            })
            $("[name='atype']").change(function () {
                var pid=$(this).val();
                $("[name='btype']").empty();
                $("[name='btype']").append("<option value=\"\"></option>");
                $("[name='ctype']").empty();
                $("[name='ctype']").append("<option value=\"\"></option>");
                $.ajax({
                    url:"typess.do",
                    data:{"pid":pid},
                    type:"post",
                    dataType:"json",
                    success:function (obj) {
                        for (let x in obj) {
                            if (obj[x].id==$("#btype").val()){
                                $("[name='btype']").append("<option value="+obj[x].id+" selected>"+obj[x].name+"</option>");
                                $("[name='btype']").trigger("change");
                            }else {
                                $("[name='btype']").append("<option value="+obj[x].id+">"+obj[x].name+"</option>");
                            }
                        }
                    }
                })
            })
            $("[name='btype']").change(function () {
                var pid=$(this).val();
                $("[name='ctype']").empty();
                $("[name='ctype']").append("<option value=\"\"></option>");
                $.ajax({
                    url:"typess.do",
                    data:{"pid":pid},
                    type:"post",
                    dataType:"json",
                    success:function (obj) {
                        for (let x in obj) {
                            if (obj[x].id==$("#ctype").val()){
                                $("[name='ctype']").append("<option value="+obj[x].id+" selected>"+obj[x].name+"</option>");
                            }else {
                                $("[name='ctype']").append("<option value="+obj[x].id+">"+obj[x].name+"</option>");
                            }
                        }
                    }
                })
            })
        })
        function add() {
            let formData = new FormData($("form")[0]);
            $.ajax({
                url:"upd.do",
                data:formData,
                type:"post",
                dataType:"json",
                success:function (obj) {
                    if (obj>0){
                        alert("修改成功");
                        location.href="list.do";
                    }else {
                        alert("修改失败");
                    }
                },
                processData:false,
                contentType:false
            })
        }
    </script>
</head>
<body>
<form enctype="multipart/form-data">
    <input type="hidden" name="sid" value="${param.sid}">
    <input type="hidden" id="atype">
    <input type="hidden" id="btype">
    <input type="hidden" id="ctype">
    商品名称:<input type="text" name="sname"><br>
    标题:<input type="text" name="title"><br>
    分类:
    <select name="atype">
        <option value=""></option>
    </select>
    <select name="btype">
        <option value=""></option>
    </select>
    <select name="ctype">
        <option value=""></option>
    </select><br>
    是否上架:<input type="radio" name="status" value="上架">上架
    <input type="radio" name="status" value="不上架">不上架<br>
    品牌:<select name="bid"><option value="">请选择</option></select><br>
    图片:<div class="photoUpLoad" >
    <input type="file"   class="photo" name="photo" accept="image/*"><span class="cross">+</span>
    <img src="" alt="" class="photoEcho">
</div><br>
    <input type="button" value="修改" onclick="add()">
</form>
</body>
</html>
